﻿Imports System.Data.SqlClient

Public Class ReservaDAO
    Public Function listar() As Collection
        Dim lista As New Collection

        Try
            Dim cn As New SqlConnection(ConexionBD.obtenerCadenaConexion())
            cn.Open()

            Dim sql = "SELECT IDRESERVA, FECHA_RESERVA, DEPORTE, CANTIDAD FROM RESERVA"
            Dim cmd As New SqlCommand(sql, cn)

            Dim dr As SqlDataReader
            dr = cmd.ExecuteReader()
            While dr.Read
                Dim modelo As New Reserva
                modelo.idReserva = dr(0).ToString
                modelo.fecha_reserva = dr(1).ToString
                modelo.deporte = dr(2).ToString
                modelo.cantidad = dr(3).ToString
                lista.Add(modelo)
            End While

            dr.Close()
            cn.Close()

        Catch ex As SqlException
            Throw New DAOExcepcion(ex.ToString)
        End Try

        Return lista
    End Function





    Public Function insertar(ByVal modelo As Reserva) As Reserva
        Try
            Dim cn As New SqlConnection(ConexionBD.obtenerCadenaConexion())
            cn.Open()

            Dim sql = "INSERT INTO RESERVA (DEPORTE, CANTIDAD, FECHA_RESERVA, EVENTO_IDEVENTO, USUARIO_IDUSUARIO) VALUES ( @VALOR2, @VALOR3, @VALOR4, @VALOR5, @VALOR6)"

            Dim cmd As New SqlCommand(sql, cn)

            cmd.Parameters.AddWithValue("@VALOR2", modelo.deporte)
            cmd.Parameters.AddWithValue("@VALOR3", modelo.cantidad)
            cmd.Parameters.AddWithValue("@VALOR4", modelo.fecha_reserva)
            cmd.Parameters.AddWithValue("@VALOR5", modelo.idevento)
            cmd.Parameters.AddWithValue("@VALOR6", modelo.idusuario)

         

            Dim afectados = cmd.ExecuteNonQuery

            If afectados <> 1 Then
                Throw New DAOExcepcion("ReservaDAO: No insertó")
            End If

            ' LEER EL ULTIMO ID
            sql = "SELECT @@IDENTITY"
            cmd = New SqlCommand(sql, cn)
            Dim dr As SqlDataReader
            dr = cmd.ExecuteReader()
            If dr.Read Then
                modelo.idreserva = CType(dr(0), Integer)
            End If

            dr.Close()
            cn.Close()

        Catch ex As Exception
            MsgBox(ex.ToString)

            Throw New DAOExcepcion(ex.ToString)
        End Try

        Return modelo

    End Function





    Public Function buscarPorFecha_reserva(ByVal fecha_reserva As String) As Collection
        Dim lista As New Collection

        Try
            Dim cn As New SqlConnection(ConexionBD.obtenerCadenaConexion())
            cn.Open()

            Dim sql = "SELECT IDRESERVA, DEPORTE, CANTIDAD, FECHA_RESERVA, EVENTO_IDEVENTO, USUARIO_IDUSUARIO FROM RESERVA WHERE UPPER(FECHA_RESERVA) LIKE @VALOR1"
            Dim cmd As New SqlCommand(sql, cn)
            cmd.Parameters.AddWithValue("@VALOR1", "%" + fecha_reserva.ToUpper + "%")

            Dim dr As SqlDataReader
            dr = cmd.ExecuteReader()

            While dr.Read
                Dim modelo As New Reserva
                modelo.idreserva = dr(0).ToString
                modelo.deporte = dr(1).ToString
                modelo.cantidad = dr(2).ToString
                modelo.fecha_reserva = dr(3).ToString
                modelo.idevento = dr(4).ToString
                modelo.idusuario = dr(5).ToString
                lista.Add(modelo)
            End While

            dr.Close()
            cn.Close()
        Catch ex As SqlException
            Throw New DAOExcepcion(ex.ToString)
        End Try

        Return lista
    End Function





    Public Sub eliminar(ByVal idreserva As String)
        Try
            Dim cn As New SqlConnection(ConexionBD.obtenerCadenaConexion())
            cn.Open()

            Dim sql = "DELETE FROM RESERVA WHERE IDRESERVA=@VALOR0"

            Dim cmd As New SqlCommand(sql, cn)
            cmd.Parameters.AddWithValue("@VALOR0", idreserva)

            Dim afectados = cmd.ExecuteNonQuery

            If afectados <> 1 Then
                Throw New DAOExcepcion("RservaDAO: No eliminó")
            End If

            cn.Close()

        Catch ex As Exception
            Throw New DAOExcepcion(ex.ToString)
        End Try
    End Sub



    Public Function obtener(ByVal idreserva As String) As Reserva
        Dim modelo As New Reserva

        Try
            Dim cn As New SqlConnection(ConexionBD.obtenerCadenaConexion())
            cn.Open()

            Dim sql = "SELECT IDRESERVA, DEPORTE, CANTIDAD, FECHA_RESERVA, EVENTO_IDEVENTO, USUARIO_IDUSUARIO FROM RESERVA WHERE IDRESERVA=@VALOR1"
            Dim cmd As New SqlCommand(sql, cn)

            cmd.Parameters.AddWithValue("@VALOR1", idreserva)

            Dim dr As SqlDataReader
            dr = cmd.ExecuteReader()
            If dr.Read Then
                modelo.idreserva = dr(0).ToString
                modelo.deporte = dr(1).ToString
                modelo.cantidad = dr(2).ToString
                modelo.fecha_reserva = dr(3).ToString
                modelo.idevento = dr(4).ToString
                modelo.idusuario = dr(5).ToString
                

            End If

            dr.Close()
            cn.Close()

        Catch ex As SqlException
            Throw New DAOExcepcion(ex.ToString)
        End Try

        Return modelo
    End Function

    Public Sub actualizar(ByVal modelo As Reserva)
        Try
            Dim cn As New SqlConnection(ConexionBD.obtenerCadenaConexion())
            cn.Open()

            Dim sql = "UPDATE RESERVA SET  DEPORTE=@VALOR2, CANTIDAD=@VALOR3, FECHA_RESERVA=@VALOR4, EVENTO_IDEVENTO=@VALOR5, USUARIO_IDUSUARIO=@VALOR6 WHERE IDRESERVA=@VALOR1"

            Dim cmd As New SqlCommand(sql, cn)
            cmd.Parameters.AddWithValue("@VALOR1", modelo.idreserva)
            cmd.Parameters.AddWithValue("@VALOR2", modelo.deporte)
            cmd.Parameters.AddWithValue("@VALOR3", modelo.cantidad)
            cmd.Parameters.AddWithValue("@VALOR4", modelo.fecha_reserva)
            cmd.Parameters.AddWithValue("@VALOR5", modelo.idevento)
            cmd.Parameters.AddWithValue("@VALOR6", modelo.idusuario)
            Dim afectados = cmd.ExecuteNonQuery

            If afectados <> 1 Then
                Throw New DAOExcepcion("ReservarDAO: No actualizó")
            End If

            cn.Close()

        Catch ex As Exception
            MsgBox(ex.Message)
            Throw New DAOExcepcion(ex.ToString)
        End Try

    End Sub
End Class
